#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "nvs_flash.h"
#include "driver/i2c_master.h"
#include "driver/spi_master.h"
#include "esp_event.h"
#include "esp_log.h"

#include "app_wifi.h"
#include "app_led_strip.h"
#include "app_e_paper.h"
#include "app_max30102.h"
#include "app_message_handler.h"
#include "app_mqtt.h"
#include "app_espnow.h"
#include "app_smartconfig.h"

#define TAG "main"

static i2c_master_bus_handle_t i2c_bus_handle;
i2c_master_bus_config_t i2c_mst_config = {
    .clk_source = I2C_CLK_SRC_DEFAULT,
    .i2c_port = I2C_NUM_0,
    .sda_io_num = 7,
    .scl_io_num = 8,
    .glitch_ignore_cnt = 7,
    .flags.enable_internal_pullup = true,
};

void app_spi_init(spi_host_device_t host_id)
{
    spi_bus_config_t buscfg = {
        .miso_io_num = -1,
        .mosi_io_num = 1,
        .sclk_io_num = 2,
        .quadwp_io_num = -1,
        .quadhd_io_num = -1,
        .max_transfer_sz = 64,
    };
    spi_bus_initialize(host_id, &buscfg, SPI_DMA_CH_AUTO);
}

void app_main(void)
{
    ESP_LOGI(TAG, "app_main start");
    nvs_flash_init();
    esp_event_loop_create_default();
    // SPI 初始化
    app_spi_init(SPI2_HOST);
    // IIC 初始化
    i2c_new_master_bus(&i2c_mst_config, &i2c_bus_handle);

    // message handler初始化
    // app_message_handler_init();

    // WIFI初始化
    app_wifi_init();

    // espnow初始化
    app_espnow_init();

    // MQTT初始化
    app_mqtt_init();

    // MAX30102初始化
    // app_max30102_init(i2c_bus_handle);

    // 墨水屏初始化
    // app_e_paper_init(SPI2_HOST);

    // led灯带初始化
    app_led_strip_init();

    vTaskDelete(NULL);
}
